package com.SmartClassroom.system.controller;

import com.SmartClassroom.system.annotation.AuthCheck;
import com.SmartClassroom.system.common.BaseResponse;
import com.SmartClassroom.system.common.DeleteRequest;
import com.SmartClassroom.system.common.ErrorCode;
import com.SmartClassroom.system.common.ResultUtils;
import com.SmartClassroom.system.constant.UserConstant;
import com.SmartClassroom.system.exception.BusinessException;
import com.SmartClassroom.system.exception.ThrowUtils;
import com.SmartClassroom.system.model.domain.LeaveRequest;
import com.SmartClassroom.system.model.domain.User;
import com.SmartClassroom.system.model.dto.leave.LeaveQueryRequest;
import com.SmartClassroom.system.model.dto.leave.LeaveStatusUpdateRequest;
import com.SmartClassroom.system.model.dto.leave.LeaveUpdateRequest;
import com.SmartClassroom.system.model.dto.user.LeaveAddRequest;
import com.SmartClassroom.system.model.vo.LeaveRequestVO;
import com.SmartClassroom.system.model.vo.LoginUserVO;
import com.SmartClassroom.system.service.LeaveRequestService;
import com.SmartClassroom.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 请假接口
 *
 * @author baijiayu
 */
@RestController
@RequestMapping("/leave")
@Slf4j
@Api(tags = {"请假接口"})
public class LeaveController {

    @Resource
    private UserService userService;

    @Resource
    private LeaveRequestService leaveService;


    // region 增删改查

    /**
     * 新增请假请求
     *
     * @param leaveAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增请假请求")
    public BaseResponse<Integer> addUser(@RequestBody LeaveAddRequest leaveAddRequest, HttpServletRequest request) {
        if (leaveAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Long id = loginUser.getId();
        LeaveRequest leaveRequest = new LeaveRequest();
        leaveRequest.setStudentId(id);
        BeanUtils.copyProperties(leaveAddRequest, leaveRequest);
        boolean result = leaveService.save(leaveRequest);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "请假请求创建失败");
        return ResultUtils.success(leaveRequest.getLeaveId());
    }

    /**
     * 删除请假请求
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除请假请求")
    public BaseResponse<Boolean> deleteLeave(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = leaveService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新请假请求
     *
     * @param leaveUpdateRequest 更新请求
     * @return BaseResponse<Boolean>
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新请假请求")
    public BaseResponse<Boolean> updateUser(@RequestBody LeaveUpdateRequest leaveUpdateRequest) {
        if (leaveUpdateRequest == null || leaveUpdateRequest.getLeaveId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新请求参数为空");
        }
        QueryWrapper<LeaveRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("leaveId", leaveUpdateRequest.getLeaveId());
        LeaveRequest leaveRequest = leaveService.getOne(queryWrapper);

        BeanUtils.copyProperties(leaveUpdateRequest, leaveRequest);
        boolean result = leaveService.updateById(leaveRequest);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新请假请求失败");
        return ResultUtils.success(result);
    }

    /**
     * 分页获取全部请假信息
     *
     * @param leaveQueryRequest 查询请求
     * @return BaseResponse<Page < LeaveRequestVO>>
     */
    @PostMapping("/list/page")
    @ApiOperation(value = "分页获取全部请假信息")
    public BaseResponse<Page<LeaveRequestVO>> listLeaveByPage(@RequestBody LeaveQueryRequest leaveQueryRequest) {
        long current = leaveQueryRequest.getCurrent() == 0 ? 1 : leaveQueryRequest.getCurrent();
        long size = leaveQueryRequest.getPageSize() == 0 ? 10 : leaveQueryRequest.getPageSize();
        Page<LeaveRequestVO> pageParam = new Page<>(current, size);
        Page<LeaveRequestVO> leaveVOPage = leaveService.getQueryWrapper(pageParam, leaveQueryRequest);
        return ResultUtils.success(leaveVOPage);
    }

    /**
     * 分页获取个人的请假列表
     *
     * @param leaveQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/my")
    @ApiOperation(value = "分页获取个人的请假列表")
    public BaseResponse<Page<LeaveRequestVO>> listMyLeaveByPage(@RequestBody LeaveQueryRequest leaveQueryRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long studentId = loginUser.getId();

        long current = leaveQueryRequest.getCurrent() == 0 ? 1 : leaveQueryRequest.getCurrent();
        long size = leaveQueryRequest.getPageSize() == 0 ? 10 : leaveQueryRequest.getPageSize();
        Page<LeaveRequestVO> pageParam = new Page<>(current, size);
        Page<LeaveRequestVO> leaveVOPage = leaveService.getQueryById(pageParam, leaveQueryRequest, studentId);
        return ResultUtils.success(leaveVOPage);
    }

    /**
     * 修改请假请求状态
     *
     * @param leaveStatusUpdateRequest 更新请求
     * @return BaseResponse<Boolean>
     */
    @PostMapping("/update/status")
    @ApiOperation(value = "修改请假请求状态")
    public BaseResponse<Boolean> updateLeaveStatus(@RequestBody LeaveStatusUpdateRequest leaveStatusUpdateRequest) {
        if (leaveStatusUpdateRequest == null || leaveStatusUpdateRequest.getLeaveId() == null || leaveStatusUpdateRequest.getStatus() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新请求参数为空");
        }

        QueryWrapper<LeaveRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("leaveId", leaveStatusUpdateRequest.getLeaveId());
        LeaveRequest leaveRequest = leaveService.getOne(queryWrapper);
        BeanUtils.copyProperties(leaveStatusUpdateRequest, leaveRequest);
        boolean result = leaveService.updateById(leaveRequest);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新请假请求状态失败");
        return ResultUtils.success(result);
    }

    // endregion
}
